package aash.catalog.views.button;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.Icon;
import javax.swing.ImageIcon;

/**
 * RoundButton. To move in SVG format.
 * @author Aashintan
 */
public class RoundButton extends AashButton {
    /**
     * serialVersionUID.
     */
    private static final long serialVersionUID = 7002046142846736226L;
    /**
     * Default radius of the round button.
     */
    private final int defaultRadius = 75;
    /**
     * Radius of the round button.
     */
    private int radius;
    /**
     * Type of the button.
     */
    private String type;

    /**
     * Class constructor.
     * @param stType of the button
     */
    public RoundButton(final String stType) {
        super();
        radius = defaultRadius;
        type = stType;
        setIcon(getTypedIcon());
        setOpaque(true);
        setBorderPainted(false);
        setFocusPainted(false);
        setContentAreaFilled(false);
    }

    /**
     * Handle if the point (x, y) is inside the circular icon.
     * @param x axis
     * @param y axis
     * @return boolean
     */
    public final boolean contains(final int x, final int y) {
        Dimension size = getSize();
        float x0 = size.width / 2F;
        float y0 = size.height / 2F;
        Icon icon = getIcon();
        float w = icon.getIconWidth() / 2F;
        float h = icon.getIconHeight() / 2F;
        boolean contains = (x - x0) * (x - x0) + (y - y0) * (y - y0) <= w * h;
        return contains;
    }

    /**
     * Return the icon of the button.
     * @return the icon corresponding to the type
     */
    private ImageIcon getTypedIcon() {
        if ((type != null) && !type.equals("")) {
            return new ImageIcon(Toolkit.getDefaultToolkit().getImage(
                "src/aash/catalog/views/button/round_button_" + type + ".png").getScaledInstance(radius,
                radius, Image.SCALE_DEFAULT));
        } else {
            return null;
        }
    }

    /**
     * Return the radius of the button.
     * @return the radius
     */
    public final int getRadius() {
        return radius;
    }

    /**
     * Set the radius of the button.
     * @param iRadius to set
     */
    public final void setRadius(final int iRadius) {
        radius = iRadius;
        setIcon(getTypedIcon());
    }

    /**
     * Set the X and Y axis.
     * @param iPosX X-axis
     * @param iPosY Y-axis
     */
    public final void setBounds(final int iPosX, final int iPosY) {
        setBounds(iPosX, iPosY, getRadius(), getRadius());
    }

    /**
     * Set the X-axis.
     * @param iPosX to set
     */
    public final void setX(final int iPosX) {
        setBounds(iPosX, (int) getBounds().getY());
    }
}
